﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Argument.Is.Not.In
{
    /// <summary>
    /// Represents a contract that adheres to <b>Argument is not in range</b> semantic.
    /// </summary>
    [Serializable]
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Method)]
    [ProvideAspectRole(Roles.Argument)]
    [ProvideAspectRole(Roles.Conditional)]
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    public sealed class RangeAttribute : ComparingContractBase
    {
        private object _minValue;
        private object _maxValue;

        /// <summary>
        /// Initializes a new instance of <b>Argument.Is.Not.In.RangeAttribute</b>.
        /// </summary>
        /// <param name="argumentName">The argument to which this contract pplies.</param>
        /// <param name="minValue">The minimum value of the range <i>argumentName</i> should not be in.</param>
        /// <param name="maxValue">The maximum value of the range <i>argumentName</i> should not be in.</param>
        /// <exception cref="System.ArgumentNullException"><i>argumentName</i> is <b>null</b>.</exception>
        public RangeAttribute(string argumentName, object minValue, object maxValue)
            : base(argumentName)
        {
            _minValue = minValue;
            _maxValue = maxValue;
        }

        /// <summary>
        /// Performs validation on the argument.
        /// </summary>
        /// <param name="argument">The argument that should be validated.</param>
        /// <returns><b>true</b> if <i>argument</i> is validated, otherwise <b>false</b>.</returns>
        protected override bool ValidateArgument(object argument)
        {
            return Comparer.Compare(argument, _minValue) < 0 || Comparer.Compare(argument, _maxValue) > 0;
        }
    }
}
